(function($) {
	var groupCode = '',groupDesc = '',locId = '',locDesc = '';
	$.init({
		swipeBack: true //启用右滑关闭功能
	});
	$.plusReady(function() {
		var currentLoc = getLocalData('currentLoc');
		if(currentLoc) {
			groupCode = currentLoc['GroupCode'];
			groupDesc = currentLoc['GroupDesc'];
			locId = currentLoc['LocId'];
			locDesc = currentLoc['LocDesc'];
		} else {
			mui.toast('用户未登录');
			mui.openWindow('logon-shell.html', 'logon-shell');
			return;
		}
		var liDate = document.getElementById('li-date');
		liDate.setAttribute('data-type','{"type":"date"}');
		var listTime = document.getElementById('li-stTime');
		listTime.setAttribute('data-type','{"type":"time"}');
		var liendtTime = document.getElementById('li-endTime');
		liendtTime.setAttribute('data-type','{"type":"time"}');
		
		if (plus.os.name == 'iOS') {
			liDate.addEventListener('tap',iosDateShow, false);
			listTime.addEventListener('tap',iosTimeShow, false);
			liendtTime.addEventListener('tap',iosTimeShow, false);
		}else{
			liDate.addEventListener('tap',liStEndDateClicked, false);
			listTime.addEventListener('tap',liStEndDateClicked, false);
			liendtTime.addEventListener('tap',liStEndDateClicked, false);
		}
		var submitBtn = document.getElementById('submitBtn');
		submitBtn.addEventListener('tap', submitBtnClicked, false);
	});
	jQuery("#textarea").on("input propertychange", function() {
		var $this = jQuery(this),
			_val = $this.val(),
			count = "";
		if(_val.length > 100) {
			$this.val(_val.substring(0, 100));
			mui.toast('最多输入100个字符')
		}
		count = $this.val().length;
		jQuery("#text-count").text(count);
	});
 
	function liStEndDateClicked() {
		var textareaE = document.getElementById('textarea');
		textareaE.blur();
		var _self = this;
		if(_self.picker) {
			_self.picker.show(function(rs) {
				var elementE = _self.querySelector('.mui-badge');
				elementE.innerText = rs.text;
				if (_self.id != 'li-date') {
					computeTimeLength(_self.id)
				}
				_self.picker.dispose();
				_self.picker = null;
			});
		} else {
			var optionsJson = _self.getAttribute('data-type');//'{"type":"time"}';
			var options = JSON.parse(optionsJson);
			_self.picker = new mui.DtPicker(options);
			_self.picker.show(function(rs) {
				var elementE = _self.querySelector('.mui-badge');
				elementE.innerText = rs.text;
				if (_self.id != 'li-date') {
					computeTimeLength(_self.id)
				}
				_self.picker.dispose();
				_self.picker = null;
			});
		}
	}
	function iosDateShow(){
		var btnDom = this
		var textareaE = document.getElementById('textarea');
		textareaE.blur();
		plus.nativeUI.pickDate(function(e) {
			var d = e.date;
			var dateStr = d.getFullYear()+'-'
			if (d.getMonth() < 9) {
				dateStr += '0'
			}
			dateStr += (d.getMonth() + 1)+'-'
			if (d.getDate() < 10) {
				dateStr += '0'
			}
			dateStr += d.getDate()
			var changeDate = btnDom.querySelector('.mui-badge');
			changeDate.innerText = dateStr;
		}, function(e) {}, {});
	}
	function iosTimeShow(){
		var btnDom = this
		var textareaE = document.getElementById('textarea');
		textareaE.blur();
		plus.nativeUI.pickTime(function(e) {
			var d = e.date;
			var dateStr = d.getHours()
			if (d.getHours() < 9) {
				dateStr = '0'+dateStr
			}
			dateStr += ':'
			if (d.getMinutes() < 10) {
				dateStr += '0'
			}
			dateStr += d.getMinutes()
			var changeDate = btnDom.querySelector('.mui-badge');
			changeDate.innerText = dateStr;
			computeTimeLength(btnDom.id)
		}, function(e) {}, {is24Hour: true});
	}
	function computeTimeLength(idStr){
		var stTimeE = document.getElementById('stTime');
		var stTime = stTimeE.innerText;
		var endTimeE = document.getElementById('endTime');
		var endTime = endTimeE.innerText;
		var overtimeLen = document.getElementById('overtimeLen');
		if (stTime == '请选择' || endTime == '请选择') {
			overtimeLen.innerText = '0小时';
			return;
		}
		if (stTime > endTime) {
			mui.toast('结束日期不能早于开始日期');
			overtimeLen.innerText = '0小时';
			return;
		}
		var stDict = getHourMinute(stTime);
		var endDict = getHourMinute(endTime);
		var hourLen = endDict['hour'] - stDict['hour'];
		var minuteLen = endDict['minute'] - stDict['minute']
		if (minuteLen == 0) {
			overtimeLen.innerText = hourLen + '小时';
			return;
		}else if (minuteLen < 0) {
			hourLen -= 1;
			minuteLen += 60;
		}
		var destFloat = hourLen + (minuteLen / 60.0);
		var destStr = getOneSmallNumberStr(destFloat);
		overtimeLen.innerText = destStr + '小时';
	}
	
	function getHourMinute(timeStr){
		var timeArr = timeStr.split(':');
		var hourStr = timeArr[0];
		if (hourStr.indexOf('0') == 0) {
			hourStr = hourStr.substring(1,hourStr.length);
		}
		var hour = parseInt(hourStr);
		var minuteStr = timeArr[1];
		if (minuteStr.indexOf('0') == 0) {
			minuteStr = minuteStr.substring(1,minuteStr.length);
		}
		var minute = parseInt(minuteStr);
		return {'hour':hour,'minute':minute};
	}
	
	function getOneSmallNumberStr(numFloat){
		var numStr = numFloat + '';
		var dropIndex = numStr.indexOf('.');
		if (numStr.length > dropIndex+2) {
			var intPart = parseInt(numStr.substr(0,dropIndex));
			var first = parseInt(numStr.substr(dropIndex+1,1));
			var second = parseInt(numStr.substr(dropIndex+2,1));
			if (second < 5) {
				if (first == 0) {
					return intPart + '';
				}
				numStr = numStr.substring(0,dropIndex+2);
				return numStr;
			}
			first += 1;
			if (first > 9 ) {
				numStr = (intPart+1) + '';
			}else{
				numStr = intPart + '.'+first;
			}
		}
		return numStr;
	}

	function submitBtnClicked() {
		var overtimeDate = document.getElementById('date');
		var overtimeDateStr = overtimeDate.innerText;
		if (overtimeDateStr == '请选择') {
			mui.toast('请选择加班日期');
			return;
		}
		var stTimeE = document.getElementById('stTime');
		var stTime = stTimeE.innerText;
		var endTimeE = document.getElementById('endTime');
		var endTime = endTimeE.innerText;
		if (stTime == '请选择') {
			mui.toast('请选择开始时间');
			return;
		}
		if (endTime == '请选择') {
			mui.toast('请选择结束时间');
			return;
		}
		if (endTime < stTime) {
			mui.toast('结束时间不能小于开始时间');
			return;
		}
		var overtimeLen = document.getElementById('overtimeLen');
		var overtimeLenStr = overtimeLen.innerText;
		overtimeLenStr = overtimeLenStr.substring(0,overtimeLenStr.length - 2);
		
		var textareaE = document.getElementById('textarea');
		var applyReason = textareaE.value;
		if (applyReason == '') {
			mui.toast('请填写申请理由');
			return
		}
		plus.nativeUI.showWaiting("正在提交",{modal:false});
		var PerId = getLocalData('pinfo').rw;
		var xmlStr = '<Request>^' + locId + '^' + PerId + '^' + overtimeDateStr + '^' +
			overtimeLenStr+ '^O^' + applyReason + '^'+stTime+'^'+endTime+'</Request>';
		postHttpRequest(xmlStr, 'nurse.schedule.nurexpectsave', function(result) {
			plus.nativeUI.closeWaiting();
			var resultDict = eval('(' + result + ')');
			if (resultDict['success'] == true) {
				plus.storage.setItem('detail','1');
				mui.toast('提交成功');
				mui.back();
			}else if (resultDict['msg'] != null || resultDict['msg'] != '') {
				mui.toast(resultDict['msg']);
			}else{
				mui.toast('提交失败,请重试');
			}
		},function(errorStr){
			mui.toast(errorStr);
			plus.nativeUI.closeWaiting();
		});
	}
	
	/*提交
	 * nurse.stop.nurleavesave
	 * rw^DepId(请假时护士所在病区)^PerId(请假护士人员Id)^LeaveType(请假类型)^StDate(请假开始日期)^EndDate(请假结束日期)^Days(请假时长(按天))^LeaveExplain(请假说明)
	 * 入参说明 rw申请时为空,LeaveType是休班代码中的rw
	 */
	
})(mui);